<?php
namespace JsonRpc\Services\System;

use JsonRpc\Models\SysOrg;
use JsonRpc\Services\CommonService;
use JsonRpc\Utils\Utils;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class OrgService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\SysOrg";
    }


    //组织查询
    public static function getOrgList($params)
    {

        $res = self::getInfoList($params);
        $data = Utils::make_treeTable($res,'id','parent_id','children',0);

        return $data;
    }

    //获取组织树
    public static function getOrgTree($params)
    {

        $params['select'] = ['id','name as text','parent_id'];
        $res = self::getInfoList($params);
        $data = Utils::make_treeTable($res,'id','parent_id','children',0);


//        foreach ($res as $key => $val) {
//            $data[$key]['id'] = $val['id'];
//            $data[$key]['text'] = $val['name'];
//            $data[$key]['state'] = 'open';
//
//            if (!empty($val)) {
//
//                $res1 = SysOrg::where('parent_id',$val['id'])->get();
//                foreach ($res1 as $k1 => $v1) {
//
//                    $data[$key]['children'][$k1]['id'] = $v1['id'];
//                    $data[$key]['children'][$k1]['text'] = $v1['name'];
//                    $data[$key]['children'][$k1]['state'] = 'open';
//
//                    if (!empty($v1)) {
//
//                        $res2 = SysOrg::where('parent_id',$v1['id'])->get();
//                        foreach ($res2 as $k2 => $v2) {
//
//                            $data[$key]['children'][$k1]['children'][$k2]['id'] = $v2['id'];
//                            $data[$key]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
//                            $data[$key]['children'][$k1]['children'][$k2]['state'] = 'open';
//
//                            if (!empty($v2)) {
//                                $res3 = SysOrg::where('parent_id',$v2['id'])->get();
//
//                                foreach ($res3 as $k3 => $v3) {
//                                    $data[$key]['children'][$k1]['children'][$k2]['children'][$k3]['id'] = $v3['id'];
//                                    $data[$key]['children'][$k1]['children'][$k2]['children'][$k3]['text'] = $v3['name'];
//                                    $data[$key]['children'][$k1]['children'][$k2]['children'][$k3]['state'] = 'open';
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }

        return $data;
    }

    //获取单条组织信息
    public function getOrgOne($params)
    {
        $info = SysOrg::find($params['id']);
        if($info){
            return $info->toArray();
        }
        return [];
    }

    //组织更新
    public function saveOrgInfo($params)
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysOrg::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SysOrg();
            $model->client_id = $user['client_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->parent_id = $params['parent_id'];
        $model->name = $params['name'];
        $model->leader = $params['leader'];
        $model->remarks = $params['remarks'];

        return $model->save();

    }

    //冻结组织
    public static function remove($params)
    {
        if (!empty($params['id'])) {
            $res = SysOrg::where('id',$params['id'])->update([
                'is_active' => '0'
            ]);
            return $res;
        }
        return [];
    }
}
